package com.handydata.antifraud.ws.redis;

import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.service.DictInfoService;
import com.handydata.antifraud.to.dap.DictTansResTO;
import com.handydata.antifraud.to.dap.DictTransReqTO;
import com.handydata.antifraud.ws.AbstractWsService;
import com.handydata.antifraud.ws.WsInvocationService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 统计上行下发的数据，返回给规则引擎用
 */
@Service("appListTransService")
public class AppListTransImpl extends AbstractWsService<DictTransReqTO, DictTansResTO>
	implements WsInvocationService<DictTransReqTO, DictTansResTO> {

	private static final Logger logger = LoggerFactory.getLogger(AppListTransImpl.class);

	@Autowired
	private DictInfoService dictInfoService;
	/**
	 * @see AbstractWsService#register()
	 */
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, DictTransReqTO.class);
	}

	@Override
	public DictTansResTO query(DictTransReqTO req) throws WsInvokeException {

		DictTansResTO res=new DictTansResTO();
		try{

			if(req.getAppList()!=null&&req.getAppList().size()>0){

				List<Object> objects=dictInfoService.getMuitleDictInfo(req.getAppList(),DictInfoCode.DICT_APP_CAT.getCode());
				Map<String, String> hashMap = new HashMap<String, String>();
				List<Object> result=new ArrayList<>();
				for(int i=0;i<objects.size();i++){

					String value=(String)objects.get(i);

					if(value!=null){
						String[] arr=value.split("_");

						hashMap.put("name",req.getAppList().get(i));
						hashMap.put("downNum",arr[0]);
						hashMap.put("type",arr[1]);
						//hashMap.put("highType",arr[2]);

						result.add(hashMap);
					}
				}
				res.setAppBase(result);
				if(req.getAppList()!=null&&req.getAppList().size()>0) {
					objects = dictInfoService.getMuitleDictInfo(req.getAppList(), DictInfoCode.DICT_APP_TYPE.getCode());
					hashMap = new HashMap<String, String>();
					result = new ArrayList<>();
					for (int i = 0; i < objects.size(); i++) {

						String value = (String) objects.get(i);
						if (value != null) {
							hashMap.put("name",req.getAppList().get(i));
							hashMap.put("type",value);
						}
					}
				}
				res.setAppType(result);
			}
			res.setResCode("0000");
			res.setResMsg("翻译手机号码所属地成功");
			return res;
		}catch (Exception ex){
			logger.error("统计上行数据出错{}",req,ex);//该抛异常还是返回错误代码？
		}
		return res;
	}
}
